package com.hrm.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hrm.dto.EmpDepDTO;
import com.hrm.entity.Department;
import com.hrm.entity.Employee;
import com.hrm.entity.MyPage;
import com.hrm.service.DepartmentService;
import com.hrm.service.EmployeeService;
import com.hrm.utils.JsonMsg;
import com.hrm.utils.Layui;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Auther: zy
 * @Date: 2020/5/27 19:25
 * @Description: 部门人员获取 获取员工带部门的信息
 */
@Controller
@RequestMapping("/department")
public class DepartmentController extends BaseController{

    @Autowired
    DepartmentService departmentService;
    @Autowired
    EmployeeService employeeService;

    /**
     * 进入部门人员统计页面
     * @return String
     */
    @RequestMapping("/depList.html")
    public String empDepList(){
        Employee employee = (Employee) session.getAttribute("user");
        Long count =(Long)employeeService.countEmp(employee.getDeptId());
        System.out.println(count);
        session.setAttribute("empNum",count);
        return "dailyManagement/departmentList";
    }

    /**
     * 跳转到员工详情页面
     * @param employee
     * @param model
     * @return String
     */
    @RequestMapping("/empInfo.html")
    public String empInfo(Employee employee, Model model){
        model.addAttribute("empInfo",employee);
        return "dailyManagement/empInfo";
    }


    /**
     * 获取员工部门列表
     * @param myPage
     * @return ResponseEntity<Layui
     */
    @RequestMapping("/empDepList.html")
    @ResponseBody
    public ResponseEntity<Layui> empDepList(MyPage myPage){
        PageHelper.startPage(myPage.getPageNum(),myPage.getPageSize());
        Employee employeeSession = (Employee) session.getAttribute("user");
        List<EmpDepDTO> empDepDTOS = departmentService.empDepList(employeeSession.getDeptId());
        PageInfo pageInfo = new PageInfo(empDepDTOS,5);
        Layui layui = Layui.data(pageInfo.getTotal(),pageInfo.getList());
        return ResponseEntity.ok(layui);
    }

    /**
     * 更新员工信息
     * @param employee
     * @return JsonMsg
     */
    @RequestMapping("/updateEmp.html")
    @ResponseBody
    public JsonMsg updateEmp(Employee employee){
        employeeService.updateEmp(employee);
        return JsonMsg.success();
    }

    /**
     * 获取部门名集合
     * @return JsonMsg
     */
    @RequestMapping("/getDeptList.html")
    @ResponseBody
    public JsonMsg getDeptList(){
        List<String> depNameList = departmentService.depNameList();
        return JsonMsg.success().addInfo("depNameList",depNameList);
    }

}
